package com.kfm.controller;

/**
 * author:hlc
 * date:2023/10/19
 */

import java.io.*;
import java.net.Socket;
import java.util.Properties;

/**
 * 客户端
 *
 * @author 17930
 */
public class TCPClient {
   public static void sendFile(String port, String path, String file, String host) {
      File file1 = new File(path,file);
      File[] files = file1.listFiles();
      for (File file2:files){
         sendFileCome(file2,port,host);
      }
   }

   public static void sendFileCome(File file,String port,String host) {
      String name = file.getName();
      try (Socket socket = new Socket(host, Integer.parseInt(port));) {
         BufferedInputStream fis = new BufferedInputStream(new FileInputStream(file));
         BufferedOutputStream fos = new BufferedOutputStream(socket.getOutputStream());
         //发送文件名称
         fos.write(name.getBytes());
         //发送文件内容
         byte[] bytes = new byte[1024 * 10 * 10];
         int len;
         while ((len = fis.read(bytes)) != -1) {
            fos.write(bytes, 0, len);
         }
         fos.flush();
         socket.shutdownOutput();
         InputStream inputStream = socket.getInputStream();
         byte[] bytes1 = inputStream.readAllBytes();
         String s = new String(bytes1);
         System.out.println(s);
         inputStream.close();
         fis.close();
      } catch (IOException e) {
         System.out.println("文件send异常");
         e.printStackTrace();
      }
   }
}
